package ch.fhnw.qualiscan.application.modals;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.elements.AdminQuestionForm;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.QuestionText;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class AdminEditQuestion extends Modal {
	
	private AdminQuestionForm caller;
	private Question question;
	private AdminModel adminModel;
	private List<Language> langList;
	private Form editForm;
	
	public AdminEditQuestion(Question question, AdminQuestionForm caller) {
		setCaption(TM.get("admin.editquestion.title"));
		this.question = question;
		this.caller = caller;
		
		adminModel = new AdminModel();
		langList = adminModel.getLanguages();
		
		buildLayout();
	}
	
	public void buildLayout() {
		editForm = new Form();
		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.setMargin(true, false, false, false);
		
		editForm.getFooter().addComponent(buttonBar);
		editForm.setCaption(TM.get("admin.editquestion.title"));
		
		for(int i = 0; i < langList.size(); i++) {
			TextField txtField = new TextField();
			txtField.setWidth("100%");
			txtField.setRows(3); 
			txtField.setColumns(20);
			txtField.setValue(adminModel.getQuestionText(question, langList.get(i)).getText());
			txtField.setCaption(langList.get(i).getLanguage());
			editForm.addField(langList.get(i).getId(), txtField);
		}
		
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				editForm.commit();
				boolean rebuild = false;
				for (int i = 0; i < langList.size(); i++) {
					if ((String)editForm.getField(langList.get(i).getId()).getValue() != "") {
						
						if (adminModel.existsQuestionText(question, langList.get(i))) {
							QuestionText qtxt = adminModel.getQuestionText(question, langList.get(i));
							qtxt.setText((String) editForm.getField(langList.get(i).getId()).getValue());
							
							if (langList.get(i).getId() == Config.ENGLISH_ID) {
								rebuild = true;
							}
							hibernateSession.getSession().flush();
						}
						else {
							
							rebuild = true;
							
							QuestionText qtxt = new QuestionText();
							qtxt.setLanguage(langList.get(i));
							qtxt.setText((String) editForm.getField(langList.get(i).getId()).getValue());
							hibernateSession.getSession().save(qtxt);
							
							Question q = (Question) hibernateSession.getSession().get(Question.class, question.getId());
							q.addQuestiontext(qtxt);
							hibernateSession.getSession().flush();
						}
					}
				}	
				hibernateSession.closeSession();
				
				if (rebuild == true) {
					caller.buildLayout();
				}
				AdminEditQuestion aed = (AdminEditQuestion) event.getButton().getData();
				aed.close();
			}
		});
		saveButton.setIcon(Config.ICON_SAVE);
		saveButton.setData(this);
		
		Button cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminEditQuestion aed = (AdminEditQuestion) event.getButton().getData();
				aed.close();
			}
		});
		cancelButton.setData(this);
		cancelButton.setIcon(Config.ICON_CANCEL);
		
		buttonBar.addComponent(saveButton);
		buttonBar.addComponent(cancelButton);
		addComponent(editForm);
	}
	
}
